package com.example.kyyl.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.kyyl.R;
import com.example.kyyl.http.HttpManager;
import com.example.kyyl.http.SPUtil;
import com.example.kyyl.utils.DateUtil;
import com.example.kyyl.entity.MeasureBgInfo;
import com.example.kyyl.entity.MsgInfo;
import com.example.kyyl.http.CallBackUtil;
import com.example.kyyl.http.ConstantUrl;
import com.example.kyyl.http.OkhttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;

import okhttp3.Call;
import rx.Subscriber;

public class MeasurePressResultActivity extends AppCompatActivity implements View.OnClickListener {
    /* access modifiers changed from: private */
    public int reTryTime;
    /* access modifiers changed from: private */
    public TextView tv_sspress;
    /* access modifiers changed from: private */
    public TextView tv_szpress;
    /* access modifiers changed from: private */
    public TextView tv_xlpress;
    String blood;
    String blood2;
    String blood3;
    private Button backButton;
//    private TextView bp_record;
    private String address="";
    private Button bt_press_exit;
    private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("action.refreshPress")) {
                MeasurePressResultActivity.this.blood = intent.getStringExtra("bloodpress1");
                MeasurePressResultActivity.this.blood2 = intent.getStringExtra("bloodpress2");
                MeasurePressResultActivity.this.blood3 = intent.getStringExtra("bloodpress3");
                MeasurePressResultActivity.this.tv_sspress.setText(MeasurePressResultActivity.this.blood);
                MeasurePressResultActivity.this.tv_szpress.setText(MeasurePressResultActivity.this.blood2);
                MeasurePressResultActivity.this.tv_xlpress.setText(MeasurePressResultActivity.this.blood3);
            }
        }
    };
//    private TextView measureDescpTv;
    private String userId = "111";

    /* access modifiers changed from: protected */
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_measurepressresult);
        initview();
        initData();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("action.refreshPress");
        registerReceiver(this.mRefreshBroadcastReceiver, intentFilter);
    }

    private void initview() {
        this.backButton = (Button) findViewById(R.id.loan_back2);
        this.bt_press_exit = (Button) findViewById(R.id.bt_press_exit);
        this.tv_sspress = (TextView) findViewById(R.id.tv_sspress);
        this.tv_szpress = (TextView) findViewById(R.id.tv_szpress);
        this.tv_xlpress = (TextView) findViewById(R.id.tv_xlpress);
//        this.bp_record = (TextView) findViewById(R.id.bp_record);
//        this.measureDescpTv = (TextView) findViewById(R.id.measure_press_description);
        this.backButton.setOnClickListener(this);
        this.bt_press_exit.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = getIntent();
        this.blood = intent.getStringExtra("bloodpress1");
        this.blood2 = intent.getStringExtra("bloodpress2");
        this.blood3 = intent.getStringExtra("bloodpress3");

        Log.i("得到", this.blood.toString());
        this.tv_sspress.setText(this.blood);
        this.tv_szpress.setText(this.blood2);
        this.tv_xlpress.setText(this.blood3);
//        String computerresult = computerresult(Integer.parseInt(this.blood), Integer.parseInt(this.blood2));
//        TextView textView = this.measureDescpTv;
//        textView.setText(getString(R.string.presstips_string) + " " + computerresult);
    }

    public String computerresult(int i, int i2) {
        return (i <= 90 || i > 120 || i2 <= 60 || i2 > 80) ? ((i > 130 || i <= 120) && (i2 <= 80 || i2 > 85)) ? (i <= 130 || i > 139 || i2 <= 85 || i2 > 89) ? (i < 140 || i > 199 || i2 <= 89 || i2 > 99) ? (i < 140 || i > 159 || i2 <= 89 || i2 > 99) ? (i <= 159 || i > 179 || i2 <= 0 || i2 > 109) ? (i < 179 || i2 <= 109) ? (i < 140 || i2 >= 90) ? (i >= 90 || i2 >= 60) ? "" : "低血压 " : "单纯收缩性高血压 " : "Ⅲ 级高血压（重度）" : " Ⅱ级高血压（中度）" : "Ⅰ级高血压（轻度）" : "Ⅰ级高血压（轻度）" : "正常高值" : "正常血压" : "理想血压";
    }

    /* access modifiers changed from: protected */
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(this.mRefreshBroadcastReceiver);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_press_exit) {
            String address=SPUtil.getString(MeasurePressResultActivity.this,"ipaddress","");
            Log.e("111111",address);
            if (address.equals("")|address==null| TextUtils.isEmpty(address)){
                Toast.makeText(MeasurePressResultActivity.this,"请在首页设置服务器地址中设置服务器地址",Toast.LENGTH_LONG).show();
            }else {
                HashMap<String,String> map=new HashMap<>();
                map.put("imei",SPUtil.getString(MeasurePressResultActivity.this,"bedid",""));
                map.put("heart",tv_xlpress.getText().toString());
                map.put("pulse",tv_xlpress.getText().toString());
                map.put("high",tv_sspress.getText().toString());//tv_szpress.getText().toString()
                map.put("low",tv_szpress.getText().toString());
                map.put("pid", SPUtil.getString(MeasurePressResultActivity.this,"pid",""));
                HttpManager.getInstance().post(address+"/api/add_blood", map, new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
//                    {"code":0,"msg":"success","data":"添加成功！"}
                        try {
                            JSONObject jsonObject=new JSONObject(s);
                            String msg=jsonObject.getString("data");
                            String m=jsonObject.getString("msg");
                            if (m.equals("success")){
                                if (msg.equals("添加成功！")){
                                    Toast.makeText(MeasurePressResultActivity.this,"上传血压测量结果成功",Toast.LENGTH_LONG).show();
                                    Intent intent=new Intent(MeasurePressResultActivity.this,MainActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
                                    startActivity(intent);
                                }else {
                                    Toast.makeText(MeasurePressResultActivity.this,"失败，请重新测量上传",Toast.LENGTH_LONG).show();
                                    Intent intent=new Intent(MeasurePressResultActivity.this,MainActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
                                    startActivity(intent);
                                }
                            }else {
                                Toast.makeText(MeasurePressResultActivity.this,"网络请求失败",Toast.LENGTH_LONG).show();
                                Intent intent=new Intent(MeasurePressResultActivity.this,MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
                                startActivity(intent);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

//            Intent intent = new Intent();
//            intent.setClass(this, MainActivity.class);
//            startActivity(intent);
//            finish();
        } else if (id == R.id.loan_back2) {
            Intent intent2 = new Intent();
            intent2.setClass(this, MainActivity.class);
            startActivity(intent2);
            finish();
        }
    }

    /* access modifiers changed from: protected */
    public void onPause() {
        super.onPause();
        reportMeasurePress(this.blood, "S");
        reportMeasurePress(this.blood2, "D");
        reportMeasurePress(this.blood3, "P");
    }

    private void reportMeasurePress(String str, String str2) {
        MeasureBgInfo measureBgInfo = new MeasureBgInfo();
        measureBgInfo.setCreateDate(ConstantUrl.dateToStr(new Date(), DateUtil.DATETIME_DEFAULT_FORMAT));
        measureBgInfo.setUserId(this.userId);
        measureBgInfo.setUnit("m");
        measureBgInfo.setValue("" + Float.parseFloat(str));
        measureBgInfo.setLastUpdateDate(ConstantUrl.dateToStr(new Date(), DateUtil.DATETIME_DEFAULT_FORMAT));
        measureBgInfo.setType(str2);
        measureBgInfo.setDevice("BPM");
        OkhttpUtil.okHttpPostJson(ConstantUrl.BASEURL + ConstantUrl.reportMeasureBloodURL, JSON.toJSONString(measureBgInfo), new CallBackUtil.CallBackString() {
            public void onFailure(Call call, Exception exc) {
                if (MeasurePressResultActivity.this.reTryTime < 3) {
//                    Toast.makeText(MeasurePressResultActivity.this, "Fail", Toast.LENGTH_LONG).show();
                    MeasurePressResultActivity.access$008(MeasurePressResultActivity.this);
                }
            }

            public void onResponse(String str) {
                MsgInfo msgInfo;
                if (!TextUtils.isEmpty(str) && (msgInfo = (MsgInfo) JSON.parseObject(str, MsgInfo.class)) != null) {
                    msgInfo.getCode();
                }
            }
        });
    }

    static /* synthetic */ int access$008(MeasurePressResultActivity measurePressResultActivity) {
        int i = measurePressResultActivity.reTryTime;
        measurePressResultActivity.reTryTime = i + 1;
        return i;
    }
}
